<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\CarBrandServices;
use think\facade\App;

/**
 * Class CarBrand
 * @package app\adminapi\controller\v1\shendeng
 */
class CarBrand extends AuthController
{
    public function __construct(App $app,CarBrandServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 列表
     * @return \think\Response
     */
    public function getList(){
        $where = $this->request->getMore([
            ['keyword',''],
            ['status',''],
        ]);
        return app('json')->success($this->services->getList($where,true));
    }

    /**
     * 获取品牌列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBrandList(){
        $where = $this->request->getMore([
            ['is_del',0],
            ['status','']
        ]);
        return app('json')->success($this->services->getBrandList($where,false));
    }

    /**
     * 添加/修改
     * @return mixed
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        return app('json')->success($this->services->add((int)$data['id']));
    }

    /**
     * 添加/修改保存
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(){
        $data = $this->request->postMore([
            ['id', 0],
            ['brand_name', ''],
            ['status', ''],
            ['sort', ''],
        ]);
        if (!$data['brand_name']) return app('json')->fail('请输入品牌名称');
        $this->services->save((int)$data['id'], $data);
        return app('json')->success('提交成功');
    }

    /**
     * 删除
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->delCity((int)$data['id']));
    }

    /**
     * 显示/隐藏
     * @param string $status
     * @param string $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setShow(string $status = '',string $id = ''){
        if ($status == '' || $id == '') return app('json')->fail('缺少参数');
        return app('json')->success($this->services->setShow((int)$id, (int)$status));
    }
}
